var grid;
var urlRead;
if (appBosch.line != null) {
    urlRead = appBosch.crud.read + '/' + appBosch.line;
} else {
    urlRead = appBosch.crud.read;
}

$(document).ready(function () {
    var dataSource = new kendo.data.DataSource({
        transport: {
            read: {
                url: urlRead,
                dataType: "json",
                complete: deleteGrid
            },
            parameterMap: function (options, operation) {
                var params = {};
                switch (operation) {
                    case 'read':
                        break;
                }
                return params;
            }
        }
    });

    grid = $("#grid").kendoGrid({
        toolbar: [
            {
                name: "pdf",
                text: "Exportar a PDF"
            },
            {
                name: "excel",
                text: "Exportar a Excel"
            }
        ],
        excel: {
            fileName: "Mis certificaciones.xlsx",
            filterable: true,
            allPages: true
        },
        pdf: {
            allPages: true,
            fileName: "Mis certificaciones.pdf"
        },
        scrollable: true,
        filterable: true,
        selectable: true,
        navigatable: true,
        resizable: true,
        columnMenu: true,
        sortable: true,
        dataSource: dataSource,
        groupable: true,
        dataBound: function () {
            appBosch.iCertifications = 1;
        },
        schema: {
            model: {
                id: "value",
                fields: {
                    start: {type: 'date'},
                    end: {type: 'date'}
                }
            }
        },
        columns: [
            {
                title: "#",
                template: '#=appBosch.iCertifications++#',
                width: 50
            },
            {
                field: "line",
                title: "Línea"
            },
            {
                field: "workstation",
                title: "Operación",
                template: '#=workstation#'
            },
            {
                field: "end",
                title: "Fecha de Certificación",
                template: '<span title="#=kendo.toString(new Date(end.replace(/-/g, "/")), "D")#">#=kendo.toString(new Date(end.replace(/-/g, "/")), "d")#</span>'
            },
            {
                field: "life",
                title: "Fecha de Vigencia",
                template: '<span title="#=kendo.toString(new Date(life.replace(/-/g, "/")), "D")#">#=kendo.toString(new Date(life.replace(/-/g, "/")), "d")#</span>'
            },
            {
                field: "proximos",
                title: " ",
                template: kendo.template($("#templateProximos").html()),
                width: 50,
                columnMenu: false
            },
            {command: [{name: "details", text: "Ver Detalles", click: showDetails}]}
        ]
    }).data('kendoGrid');
    $('#grid table').on('dblclick', 'tr', showDetails2);
    $('body').on('click', '.seeCert', showCertificate);
});

function showDetails(e) {
    var dataItem = this.dataItem($(e.currentTarget).closest("tr"));
    var id = dataItem.value;
    var contentTemp = $("#templateDetalles").html();
    var temp = kendo.template(contentTemp);

    $.get(appBosch.urlDetails + '/' + id, {}, function (data) {
        var window = $("<div></div>");
        var info = {data: data};
        window.html(temp(info));
        window.append('<input class="seeCert k-button k-button-icontext" data-id="' + id + '" value="Ver Certificado" />');
        window.kendoWindow({
            modal: true,
            minWidth: "1000px",
            title: dataItem.line + ' - ' + dataItem.workstation,
            actions: [
                "Close"
            ]
        });
        window.bind("deactivate", function () {
            window.destroy();
        });
        window.data("kendoWindow").open().center();
    });
}

function showDetails2() {
    var selectedRows = grid.select();
    var dataItem = grid.dataItem(selectedRows[0]);
    var id = dataItem.value;
    var contentTemp = $("#templateDetalles").html();
    var temp = kendo.template(contentTemp);

    $.get(appBosch.urlDetails + '/' + id, {}, function (data) {
        var window = $("<div></div>");
        var info = {data: data};
        window.html(temp(info));
        window.append('<input class="seeCert k-button k-button-icontext" data-id="' + id + '" value="Ver Certificado" />');
        window.kendoWindow({
            modal: true,
            minWidth: "1000px",
            title: dataItem.line + ' - ' + dataItem.workstation,
            actions: [
                "Close"
            ]
        });
        window.bind("deactivate", function () {
            window.destroy();
        });
        window.data("kendoWindow").open().center();
    });
}

function showCertificate() {
    var idCert = $(this).data('id');
    var url = appBosch.urlCertificate + '/' + idCert;
    var magicLink = $('#magicLink');
    magicLink.attr('href', url);
    magicLink.attr('target', '_blank');
    magicLink[0].click();
}

function deleteGrid(e) {
    if (e.responseText == '[]') {
        $("#messageCert").removeClass('hidden');
    }
}